<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class QuestionsController  extends AppController {
	
	public function beforeFilter() {
            $this->layout = "backend"; 
            parent::beforeFilter();
    					
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {		
		$isGroup =$this->Auth->user('Group');	
		$this->Question->recursive = 0;
		if($isGroup['name']=="Superusers"){
			$conditions = array();
		}else{
			$conditions = array('Question.id_user'=>$this->Auth->user('id'));
		}
		
        $this->paginate = array(
		        'conditions'=>$conditions,
		        'order'=>'Question.order asc'
	       	 );
		$this->set('questions', $this->paginate('Question'));
        $this->render('/Backend/Questions/index');
	}



/**
 * add method
 *
 * @return void
 */
	public function add() {
                  
		if ($this->request->is('post')) {		
		
			$maxOrder = $this->Question->find('first', array(				 
				    'fields' => array('MAX(Question.order) AS _next', '*'), 
				   ));
			if($maxOrder[0]['_next']){
				$order = $maxOrder[0]['_next']+1;
			}else{
				$order = 1;
			}  
			$this->request->data['Question']['id_user']= $this->Auth->user('id'); 
			$this->request->data['Question']['type']= $this->Auth->user('mobile_no');  
			$this->request->data['Question']['order']= $order;   
			$this->request->data['Question']['time_start']=  date("Y-m-d H:i:s", strtotime($this->request->data['Question']['time_start']));
			$this->request->data['Question']['time_end']=  date("Y-m-d H:i:s", strtotime($this->request->data['Question']['time_end']));
			
			$this->Question->create();
			if ($this->Question->save($this->request->data)) {
				$this->Session->setFlash(__('Bạn vừa thêm câu hỏi'));
				$this->redirect(array('action' => 'index'));
			} else {
				//$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
                $this->render('/Backend/Questions/add');
		
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Question->id = $id;
		if (!$this->Question->exists()) {
			throw new NotFoundException(__('Không tồn tại câu hỏi'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Question->save($this->request->data)) {
				$this->Session->setFlash(__('Bạn sửa thành công câu hỏi'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Bạn không sưa thành công, vui lòng thử lại'));
			}
		} else {
			$this->request->data = $this->Question->read(null, $id);
		}
		  $this->render('/Backend/Questions/edit');
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Question->id = $id;
		if (!$this->Question->exists()) {
			throw new NotFoundException(__('Không tồn tại câu hỏi'));
		}
		if ($this->Question->delete()) {
                        $this->loadModel("Answer");
                        $condition= array('Answer.id_quyestion'=>$id);
                        $this->Answer->deleteAll($condition,false);
			$this->Session->setFlash(__('Bạn đã xóa câu hỏi'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Bạn không xóa được câu hỏi vui lòng thử lại'));
		$this->redirect(array('action' => 'index'));
	}
	public function block($id = null) {
		$this->Question->id = $id;		
		if (!$this->Question->exists()) {
            throw new NotFoundException(__('Không tồn tại câu hỏi'));
        }
	  if ($this->Question->id) {
	  		  $data= array('Question'=> array('id' =>$this->Question->id,'status' => '0'));
		  if($this->Question->saveall($data)){
	                $this->Session->setFlash(__('Bạn vừa block câu hỏi'));
	                $this->redirect($this->referer());
	      }else{
	      	 		$this->Session->setFlash(__('Không thành công, vui lòng thử lại'));
	                $this->redirect($this->referer());
	      }		  		
        }else{
        	$this->redirect($this->referer());
        }
	}
        
	public function open($id = null) {
		$this->Question->id = $id;		
		if (!$this->Question->exists()) {
            throw new NotFoundException(__('Không tồn tại câu hỏi'));
        }
	  if ($this->Question->id) {
	  		  $data= array('Question'=> array('id' =>$this->Question->id,'status' => '1'));
		  if($this->Question->saveall($data)){
	                $this->Session->setFlash(__('Bạn vừa open thành công câu hỏi'));
	                $this->redirect($this->referer());
	      }else{
	      	 		$this->Session->setFlash(__('Xin vui lòng thử lại'));
	                $this->redirect($this->referer());
	      }		  		
        }else{
        	$this->redirect($this->referer());
        }
	}
        public function end($id = null) {
		$this->Question->id = $id;		
			if (!$this->Question->exists()) {
	            throw new NotFoundException(__('Không tồn tại câu hỏi'));
	        }
		  if ($this->Question->id) {
		  		  $data= array('Question'=> array('id' =>$this->Question->id,'question_end' => '1'));
			  if($this->Question->saveall($data)){
		                $this->Session->setFlash(__('Bạn đã chọn câu hỏi này làm câu cuối'));
		                $this->redirect($this->referer());
		      }else{
		      	 		$this->Session->setFlash(__('Vui long thử lại'));
		                $this->redirect($this->referer());
		      }		  		
	        }else{
	        	$this->redirect($this->referer());
	        }
		}
	public function enddel($id = null) {
			$this->Question->id = $id;		
				if (!$this->Question->exists()) {
		            throw new NotFoundException(__('Không tồn tại câu hỏi'));
		        }
			  if ($this->Question->id) {
			  		  $data= array('Question'=> array('id' =>$this->Question->id,'question_end' => '0'));
				  if($this->Question->saveall($data)){
			                $this->Session->setFlash(__('Hủy thành công'));
			                $this->redirect($this->referer());
			      }else{
			      	 		$this->Session->setFlash(__('Vui lòng thử lại'));
			                $this->redirect($this->referer());
			      }		  		
		        }else{
		        	$this->redirect($this->referer());
		        }
			}
                        
	public function deleteAllMember(){
		$this->loadModel('Member'); 
		$datas = $this->Member->find('all');		
		foreach($datas as $key=>$value){
			 $this->Member->id = $value['Member']['id'];
			 $this->Member->delete();
        	 
		}
                 $this->Session->setFlash(__('Bạn đã hủy thành công số người tham gia'));
		 $this->redirect(array('action' => 'index'));
		
	}
        public function deleteAllQA(){
		$this->loadModel('QuestionAnswer'); 
		
		$datas = $this->QuestionAnswer->find('all');		
		foreach($datas as $key=>$value){
			 $this->QuestionAnswer->id = $value['QuestionAnswer']['id'];
			 $this->QuestionAnswer->delete();
        	 
		}
                $this->Session->setFlash(__('Bạn đã hủy thành công kết quả trả lời'));
		 $this->redirect(array('action' => 'index'));
	}
	public function finish() {
		$datas = $this->Question->find('list',array('conditions'=>array('Question.status'=>1),
													'fields' => array('Question.id'),
													));
		
		if(count($datas)>0){
			foreach($datas as $key=>$data){
				$up[$key]=array('Question'=>array('id'=>$key,'status'=>2));
			}
			if($this->Question->saveall($up)){                
                $this->Session->setFlash(__('Kết thúc hội thảo'), 'success');
                $this->redirect($this->referer());
             }else{             	
             	 $this->Session->setFlash(__('Vui lòng thử lại'), 'error');
                $this->redirect($this->referer());
             }
		}else{
			 $this->redirect($this->referer());
		}
	}
        public function down($id = null,$order=null) {
            $this->Question->id = $id;		
            if (!$this->Question->exists()) {
                throw new NotFoundException(__('Không tồn tại câu hỏi'));
                
            }if ($this->Question->id) {               
                $one = $this->Question->find('all',array(
                    'conditions'=>array(
                        'Question.status'=>1,
                        'Question.order >'=>$order,
                    ),
                    'order'=>'Question.order asc',
                    'limit'=>1
                    ));
                
                $next = array('Question'=>array(
                        'id'=>  $this->Question->id,
                        'order'=>$one[0]['Question']['order']
                ));
               
                $back =array('Question'=>array(
                        'id'=>  $one[0]['Question']['id'],
                        'order'=>$order
                ));
                if($this->Question->save($next)&& $this->Question->save($back) ){
                      $this->Session->setFlash(__('Thanh công'));
                      $this->redirect($this->referer());
                }else{
                    $this->Session->setFlash(__('Vui long thử lại'));
                    $this->redirect($this->referer());
                }		  		
	        }else{
	        	$this->redirect($this->referer());
	        }
	}
        public function up($id = null,$order=null) {
            $this->Question->id = $id;		
            if (!$this->Question->exists()) {
                throw new NotFoundException(__('Không tồn tại câu hỏi'));
                
            }if ($this->Question->id) {               
                $one = $this->Question->find('all',array(
                    'conditions'=>array(
                        'Question.status'=>1,
                        'Question.order <'=>$order,
                    ),
                    'order'=>'Question.order DESC',
                    'limit'=>1
                    ));
               
                $next = array('Question'=>array(
                        'id'=>  $this->Question->id,
                        'order'=>$one[0]['Question']['order']
                ));
               
                $back =array('Question'=>array(
                        'id'=>  $one[0]['Question']['id'],
                        'order'=>$order
                ));
                if($this->Question->save($next)&& $this->Question->save($back) ){
                      $this->Session->setFlash(__('Thanh công'));
                      $this->redirect($this->referer());
                }else{
                    $this->Session->setFlash(__('Vui long thử lại'));
                    $this->redirect($this->referer());
                }		  		
	        }else{
	        	$this->redirect($this->referer());
	        }
	}
}
